using System;
using System.Xml.Linq;

namespace UtilsExtensionsAndHelpers.Helpers
{
    public interface ISerializationHelper
    {
        /// <summary>
        /// Serializes an object.
        /// </summary>
        /// <param name="model">The object you want to Serialize</param>
        /// <returns>The serialized xml in the form of a string.</returns>
        String Serialize(object model);

        /// <summary>
        /// Serializes an object.
        /// </summary>
        /// <param name="model">The object you want to Serialize</param>
        /// <param name="errorHandler">What to do with an exception, return true to rethow, false to swallow</param>
        /// <returns>The serialized xml in the form of a string.</returns>
        String Serialize(object model, Func<Exception,bool> errorHandler);

        /// <summary>
        /// Serialize an object to a file.
        /// </summary>
        /// <param name="model">The object you want to serialize.</param>
        /// <param name="filePath">The name of the file you want to serialize to.</param>
        void SerializeToFile(object model, string filePath);

        /// <summary>
        /// Serialize an object to a file.
        /// </summary>
        /// <param name="model">The object you want to serialize.</param>
        /// <param name="filePath">The full path of the file you want to serialize to.</param>
        /// <param name="errorHandler">What to do with an exception, return true to rethow, false to swallow</param>
        void SerializeToFile(object model, string filePath, Func<Exception,bool> errorHandler);

        /// <summary>
        /// Deserializes an object.
        /// </summary>
        /// <param name="xmlSerializedObject">The xml serialized object you want to deserialize</param>
        /// <returns>The deserialized object.</returns>
        T Deserialize<T>(string xmlSerializedObject);

        /// <summary>
        /// Deserializes an object.
        /// </summary>
        /// <param name="xmlSerializedObject">The object you want to Deserialize</param>
        /// <param name="errorHandler">What to do with an exception, return true to rethow, false to swallow</param>
        /// <returns>The deserialized object.</returns>
        T Deserialize<T>(string xmlSerializedObject, Func<Exception, bool> errorHandler);

        T Deserialize<T>(XElement xmlSerializedObject);
        T Deserialize<T>(XElement xmlSerializedObject, Func<Exception, bool> errorHandler);

        /// <summary>
        /// Deserializes an object.
        /// </summary>
        /// <param name="filePath">The path to the serialized xml file</param>
        /// <returns>The deserialized object.</returns>
        T DeserializeFromFile<T>(string filePath);

        /// <summary>
        /// Deserializes an object.
        /// </summary>
        /// <param name="filePath">The path to the serialized xml file</param>
        /// <param name="errorHandler">A method to handles error return true to rethrow or false to swallow</param>
        /// <returns>The deserialized object.</returns>
        T DeserializeFromFile<T>(string filePath, Func<Exception, bool> errorHandler);

        /// <summary>
        /// Converts UTF8 Byte Array to a string. This is used in the serialize 
        /// method so I can write to a memory stream instead of a file.
        /// </summary>
        String UTF8ByteArrayToString(Byte[] characters);

        /// <summary>
        /// Converts a string to a UTF8 Byte Array. This is This is used in the 
        /// deserialize method so I can read from a memory stream instead of a file.
        /// </summary>
        Byte[] StringToUTF8ByteArray(String pXmlString);
    }
}